<script setup>
import { ref, onMounted } from 'vue';
import { useInfoStore } from '@/stores/info';
const isEditing = ref(false);
const store = useInfoStore();
import {userInfoGetService, userInfoUpdateService} from "@/api/user.js";
import {ElMessage} from "element-plus";

const currentInfo = ref({
  nickname: '',
  gender: '',
  birthday: '',
  location: '',
  description: ''
});
const getUserInf = async ()=>{
  let result = await userInfoGetService();
  //存储pinia
  store.setInfo(result.data);
}
onMounted(() => {
  currentInfo.value = {...store.userInfo};
  getUserInf()
});
const toggleEdit = () => {
  isEditing.value = !isEditing.value;
  if (!isEditing.value) {
    currentInfo.value = {...store.userInfo};
  }
};
const saveEdit = async () => {
  let result = await userInfoUpdateService(currentInfo.value)
  ElMessage.success(result.message? result.message:'修改成功')
  store.userInfo = { ...currentInfo.value };
  isEditing.value = false;
};
</script>

<template>
  <div class="settings-content">
    <div class="content-header">
      <h2>{{ $t("setting2") }}</h2>
      <p>{{ $t('infoSettingsDescription') }}</p>
    </div>
    <div class="settings-card">
      <div class="card-header">
        <h3><i class="fa-solid fa-circle-info"></i>{{ $t("personalInfo") }}</h3>
        <div class="button-group">
          <button v-if="isEditing" class="cancel-button" @click="toggleEdit">{{ $t("cancel") }}</button>
          <button :class="isEditing ? 'save-button' : 'edit-button'" @click="isEditing ? saveEdit() : toggleEdit()">
            {{ isEditing ? $t("save") : $t("edit") }}
          </button>
        </div>
      </div>
      <div class="settings-grid">
        <div class="settings-item">
          <div class="info-label">{{ $t("nickname") }}</div>
          <div class="info-value">
            <span v-if="!isEditing">{{ currentInfo.nickname || $t("notSet") }}</span>
            <input v-else v-model="currentInfo.nickname" type="text" class="edit-input">
          </div>
        </div>

        <div class="settings-item">
          <div class="info-label">{{ $t("gender") }}</div>
          <div class="info-value">
            <span v-if="!isEditing">{{ currentInfo.gender === "男" ? $t("male") : $t("female") || $t("notSet") }}</span>
            <select v-else v-model="currentInfo.gender" class="edit-input">
              <option :value="'男'">{{ $t("male") }}</option>
              <option :value="'女'">{{ $t("female") }}</option>
            </select>
          </div>
        </div>

        <div class="settings-item">
          <div class="info-label">{{ $t("birthday") }}</div>
          <div class="info-value">
            <span v-if="!isEditing">{{ currentInfo.birthday || $t("notSet") }}</span>
            <input v-else v-model="currentInfo.birthday" type="date" class="edit-input">
          </div>
        </div>

        <div class="settings-item">
          <div class="info-label">{{ $t("uAddress") }}</div>
          <div class="info-value">
            <span v-if="!isEditing">{{ currentInfo.location || $t("notSet") }}</span>
            <input v-else v-model="currentInfo.location" type="text" class="edit-input">
          </div>
        </div>

        <div class="settings-item">
          <div class="info-label">{{ $t("description") }}</div>
          <div class="info-value">
            <span v-if="!isEditing">{{ currentInfo.description || $t("notSet") }}</span>
            <textarea v-else v-model="currentInfo.description" class="edit-textarea"></textarea>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.settings-content {
  background: var(--bgcolor);
  border-radius: 16px;
  box-shadow: 0 6px 25px rgba(0, 0, 0, 0.07);
  padding: 40px;
  min-height: 500px;
  max-width: 1000px;
  margin-left: calc(50vw - 500px);
  color: var(--txcolor);
  border: 1px solid var(--border-color);
}

.content-header {
  margin-bottom: 30px;
}

.content-header h2 {
  font-size: 1.8rem;
  color: var(--header-color);
  margin-bottom: 10px;
}

.content-header p {
  color: var(--secondary-text);
  font-size: 1rem;
  max-width: 600px;
  line-height: 1.6;
}

.settings-card {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 25px;
  margin-bottom: 25px;
  border: 1px solid var(--border-color);
}

.settings-card h3 {
  font-size: 1.2rem;
  color: var(--header-color);
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.settings-card h3 i {
  color: var(--accent-color);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.button-group {
  display: flex;
  align-items: center;
  margin-top: 10px;
  margin-right: 10px;
}

.button-group button {
  padding: 6px 20px;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
  border: none;
  margin-left: 8px;
}

.edit-button {
  background-color: var(--button-bg);
  color: white;
}

.cancel-button {
  background-color: #94a3b8;
  color: white;
}

.cancel-button:hover {
  background-color: #64748b;
}

.save-button {
  background-color: #10b981;
  color: white;
}

.save-button:hover {
  background-color: #059669;
}

.settings-item {
  display: flex;
  align-items: flex-start;
  padding: 15px 0;
  border-bottom: 1px solid #e2e8f0;
}

.settings-item:last-child {
  border-bottom: none;
}

.info-label {
  width: 120px;
  font-weight: 500;
  color: var(--h4-color);
  padding-top: 5px;
}

.info-value {
  flex: 1;
  color: var(--txcolor);
  min-height: 24px;
  padding-top: 5px;
}

.edit-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.2s;
  background-color: var(--select-color);
  color: var(--txcolor);
}

.edit-input:focus {
  border-color: #3b82f6;
  outline: none;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.edit-textarea {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  font-size: 14px;
  min-height: 80px;
  resize: vertical;
  transition: border-color 0.2s;
  background-color: var(--select-color);
  color: var(--txcolor);
}

.edit-textarea:focus {
  border-color: #3b82f6;
  outline: none;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}
</style>